<?php
class model_product extends base_model{
	protected $input_type = array(
		0 => '输入框',
		1 => '单选',
		2 => '多选',
		3 => '多行输入',
		4 => '下拉选择',
	);
	private $result = array(
		'state' => 404,
		'message' => 'error',
	);
	
	/*
	* 取出指定ID分类
	* @param int $id 要取分类的ID
	*/
	public function mod_category_item($id){
		$sql = "SELECT * FROM `@@product_category` WHERE `id`={$id} LIMIT 1";
		$data = $this->db->query($sql,2);
		return $data;
	}
	/*
	* 取出所有的主分类
	* return array
	*/
	public function mod_parentcategory(){
		$sql = "SELECT * FROM `@@product_category` WHERE `parentid`=0";
		$data = $this->db->query($sql,3);
		return $data;
	}
	/*
	* 取出所有的分类
	*/
	public function mod_category_list($args){
	    $where = '';
	    $subwhere = '';
        $parentids = '';
        if(!empty($args['sKey'])){
            $where .= " AND `title` LIKE '%".strval($args['sKey'])."%'";
            $subwhere .= " AND `title` LIKE '%".strval($args['sKey'])."%'";
        }
        if(!empty($args['categoryid'])){
            $parentids .= ' AND `id`='.intval($args['categoryid']);
        }
        //找出主分类
        if(!empty($where)) {
            $sql = "SELECT GROUP_CONCAT(DISTINCT `parentid`) AS parentid FROM `@@product_category` WHERE 1 {$where}";
            $sd = $this->db->query($sql, 2);
            if($sd && !empty($sd['parentid'])){
                $parentids = ' AND `id` IN('.$sd['parentid'].')';
            }
        }

		$sql = "SELECT * FROM `@@product_category` WHERE `parentid`=0 {$parentids}";
		$data = $this->db->query($sql,3);
		foreach($data as $k=>$v){
            $subwhere .= ' AND `parentid`='.intval($v['id']);
            $data[$k]['subdata'] = $this->mod_sub_category_search($subwhere);
            $subwhere = '';
		}
		return $data;
	}
    /*
    * 取出指定（搜索）子分类
    * @param int $where
    * @param string $field 要返回的字段列表
    * return array
    */
    public function mod_sub_category_search($where,$field='*'){
        $sql = "SELECT {$field} FROM `@@product_category` WHERE 1 {$where} order by `parentid`,`sort` ASC";
        $data = $this->db->query($sql,3);
        return $data;
    }
	/*
	* 取出所有子分类
	* @param int $id 主分类的ID
	* @param string $field 要返回的字段列表
	* return array
	*/
	public function mod_sub_category($id,$field='*'){
		$sql = "SELECT {$field} FROM `@@product_category` WHERE `parentid`={$id} order by `parentid`,`sort` ASC";
		$data = $this->db->query($sql,3);
		return $data;
	}
	

	/*
	* 保存添加或编辑的分类
	*/
	public function mod_category_save($para,$update_args=null,$edit=false){
		$result = 0;
		if($edit){
			$result = $this->db->update('@@product_category',$para,$update_args);
		}else{
			$result = $this->db->insert('@@product_category',$para);
		}
		return $result;
	}
	/*
	* 更新分类状态
	* @param int $id 分类ID
	*/
	public function mod_category_state($id){
		$sql = "UPDATE @@product_category SET `state`=ABS(`state`-1) WHERE `id`={$id}";
		$result = $this->db->query($sql,0);
		return $result;
	}
	
	/////////////////////////////////////////////////////////////////////////////////////
	// 商品品牌库 Begin
	/////////////////////////////////////////////////////////////////////////////////////
    /*
     * 删除指定商品品牌
     */
	public function mod_brands_delete($id){
	    $sql = 'DELETE FROM `@@brands` WHERE `id`='.$id;
	    return $this->db->query($sql,0);
    }
    /*
     * 删除选定商品品牌
     */
    public function mod_brands_delete_list($ids){
        $sql = 'DELETE FROM `@@brands` WHERE `id` in ('.$ids.')';
        return $this->db->query($sql,0);
    }
    /*
     * 商品品牌列表
     * @param array $args 参数列表
     */
	public function mod_product_brands($args){
        $where_txt = "";
        if(!empty($args['categoryid'])){
            $where_txt .= " AND `categoryid`=".$args['categoryid'];
        }
        if(!empty($args['sKey'])){
            $where_txt .= " AND `title` LIKE '%{$args['sKey']}%'";
        }

		$sql = "SELECT ~field~ FROM `@@brands` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => '*',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => '',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
	}
	/*
	 * 取出单行品牌数据记录
	 * @param int $id 品牌ID
	 */
	public function mod_brands_item($id){
        $sql = 'SELECT * FROM `@@brands` WHERE `id`='.$id.' LIMIT 1';
        return $this->db->query($sql,2);
    }


	/////////////////////////////////////////////////////////////////////////////////////
	// 商品品牌库 End
	/////////////////////////////////////////////////////////////////////////////////////
	
	//////////////////////////////////////
	// 商品属性
	/////////////////////////////////////
	public function mod_attribute_item($id){
		$sql = 'SELECT * FROM `@@product_attribute` WHERE `id`='.$id.' LIMIT 1';
        return $this->db->query($sql,2);
	}
	public function mod_attribute_list($id,$subid){
	    $sql = 'SELECT * FROM `@@product_attribute` WHERE `categoryid`='.$id.' and `categorysubid`='.$subid;
        return $this->db->query($sql,3);
    }
	public function mod_product_attribute($args){
		$where_txt = "";
        if(!empty($args['categoryid'])){
            $where_txt .= " AND `categoryid`=".$args['categoryid'];
        }
		if(!empty($args['categorysubid'])){
            $where_txt .= " AND `categorysubid`=".$args['categoryid'];
        }
        if(!empty($args['sKey'])){
            $where_txt .= " AND `title`='{$args['sKey']}'";
        }

		$sql = "SELECT ~field~ FROM `@@product_attribute` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => '*',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => '',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
	}

	/*
	 * 运费模板
	 */
	public  function mod_product_freight_template($args){
        $where_txt = "";
        if(!empty($args['sKey'])){
            $where_txt .= " AND `title`='{$args['sKey']}'";
        }

        $sql = "SELECT ~field~ FROM `@@freight_template` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => '*',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => '',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
    }
    /*
     * 删除指定ID的模板
     * @param int $id 模板ID
     */
    public  function mod_freight_template_delete($id){
        return $this->db->delete('@@freight_template',array('id'=>$id));
    }
    /*
     * 商品列表
     */
    public  function mod_product_products($args){
        $where_txt = "";
        if(!empty($args['sKey'])){
            $where_txt .= " AND `title`='{$args['sKey']}'";
        }

        $sql = "SELECT ~field~ FROM `@@products` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => '*',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => '',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
    }
    //删除指定ID的商品数据
    public function mod_products_delete($id,$code){
        $result = 0;
        $sql = 'DELETE FROM `@@products` WHERE `id`='.$id;
        $result = $this->db->query($sql,0);
        if($result){
            //删除参数
            $sql = "DELETE FROM `@@products_attr` WHERE `code`='{$code}'";
            $result = $this->db->query($sql,0);
            //删除详情
            $sql = "DELETE FROM `@@products_con` WHERE `code`='{$code}'";
            $result = $this->db->query($sql,0);
        }
        return $result;
    }
    //取出用户等级
    public function mod_user_vip(){
        $sql = "SELECT `gid`,`title` FROM `@@member_vip` ORDER BY `gid` ASC";
        $data = $this->db->query($sql,3);
        $args = array();
        foreach ($data as $v){
            $args[$v['gid']] = $v['title'];
        }
        return $args;
    }
    //取出指定ID的商品数据
    public function mod_product_item($code){
        $sql = "SELECT * FROM `@@products` WHERE `code`='".strval($code)."'";
        $data = $this->db->query($sql,2);
        //找出对应的属性
        $sql = "SELECT * FROM `@@products_attr` WHERE `code`='".strval($code)."'";
        $attr = $this->db->query($sql,3);
        $attribute = array();
        foreach ($attr as $k=>$v){
            $attribute[$v['forid']] = $v['attval'];
        }
        //找出对应的详情
        $sql = "SELECT * FROM `@@products_con` WHERE `code`='".strval($code)."'";
        $cont = $this->db->query($sql,2);
        return array(
            'data' => $data,
            'attr' => $attribute,
            'cont' => $cont,
        );
    }
}